import json
import time

import requests

from confluence_api import ConfluenceApi

c = ConfluenceApi(
    'https://ar.intra.nsfocus.com',
    'ar1',
    'tpo@31580')
link_list = [
    'https://qrl146y7.r.us-east-1.awstrack.me/L0/https:%2F%2Fforrester.com%2Freport%2Fhow-to-choose-the-right-partner-for-your-experience-transformation%2FRES177703%3Futm_source=forrester_smtp%26utm_medium=email%26utm_campaign=research%2520alert/1/02000000g4u0gccp-ckf9pcag-h50s-uomr-ib44-6gn1v2uii5g0-000000/BpmbzGk2eYLYKpeWUdFj1O6jAk8=299',
    'https://qrl146y7.r.us-east-1.awstrack.me/L0/https:%2F%2Fforrester.com%2Freport%2Fthe-forrester-wave-security-awareness-and-training-solutions-q1-2022%2FRES176325%3Futm_source=forrester_smtp%26utm_medium=email%26utm_campaign=research%2520alert/1/02000000g4u0gccp-ckf9pcag-h50s-uomr-ib44-6gn1v2uii5g0-000000/u9YPFqWbW_dzDVRX3gF0zX0RT-I=299'
]
a = 0
for link in link_list:
    a += 1
    # link = input("请输入爬取链接：")

    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36',
        'Connection': 'close'
    }
    data = json.dumps({'link': link})
    s = requests.session()
    s.keep_alive = False
    # req = requests.post('http://192.168.19.16:9111/arForresterPdfSpider', data=data, headers=headers)
    # time.sleep(5)
    # if req.json()['code'] == 200:
    #     print(req.json())
    #     print('---------第%s篇成功' % a)
    # else:
    #     print(req.json(), '--------第%s篇失败' % a)

    # 提取ar阅览室链接
    req2 = requests.post('http://192.168.19.16:9111/isForresterLinkInAR', data=data, headers=headers)
    print('%s--调用接口' % a)
    time.sleep(300)
    if req2.json()['code'] == 201:
        # print(req2.json())
        ar_url = req2.json()['data']
        print(ar_url)
        print('---------第%s篇成功' % a)
        # time.sleep(180)
    else:
        print(req2.json())
        print('--------第%s篇失败' % a)
    # elif req2.json()['code'] == 200:
    #     print(req2.json(), '----------------爬取失败,再次爬取')
    #     time.sleep(180)
    #     req3 = requests.post('http://192.168.19.16:9111/isForresterLinkInAR', data=data, headers=headers)
    #     ar_url = req3.json()['data']
    #     print(ar_url)
    #     time.sleep(180)